<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>loggerclient.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_messaging_loggerclient.js.html">loggerclient.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This class sends logging messages over a message channel to a
<a name="line17"></a> * server on the main page that prints them using standard logging mechanisms.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.messaging.LoggerClient&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line24"></a>goog.require(&#39;goog.debug&#39;);
<a name="line25"></a>goog.require(&#39;goog.debug.LogManager&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Creates a logger client that sends messages along a message channel for the
<a name="line32"></a> * remote end to log. The remote end of the channel should use a
<a name="line33"></a> * {goog.messaging.LoggerServer} with the same service name.
<a name="line34"></a> *
<a name="line35"></a> * @param {!goog.messaging.MessageChannel} channel The channel that on which to
<a name="line36"></a> *     send the log messages.
<a name="line37"></a> * @param {string} serviceName The name of the logging service to use.
<a name="line38"></a> * @constructor
<a name="line39"></a> * @extends {goog.Disposable}
<a name="line40"></a> */
<a name="line41"></a>goog.messaging.LoggerClient = function(channel, serviceName) {
<a name="line42"></a>  if (goog.messaging.LoggerClient.instance_) {
<a name="line43"></a>    return goog.messaging.LoggerClient.instance_;
<a name="line44"></a>  }
<a name="line45"></a>
<a name="line46"></a>  goog.base(this);
<a name="line47"></a>
<a name="line48"></a>  /**
<a name="line49"></a>   * The channel on which to send the log messages.
<a name="line50"></a>   * @type {!goog.messaging.MessageChannel}
<a name="line51"></a>   * @private
<a name="line52"></a>   */
<a name="line53"></a>  this.channel_ = channel;
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * The name of the logging service to use.
<a name="line57"></a>   * @type {string}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.serviceName_ = serviceName;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * The bound handler function for handling log messages. This is kept in a
<a name="line64"></a>   * variable so that it can be deregistered when the logger client is disposed.
<a name="line65"></a>   * @type {Function}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.publishHandler_ = goog.bind(this.sendLog_, this);
<a name="line69"></a>  goog.debug.LogManager.getRoot().addHandler(this.publishHandler_);
<a name="line70"></a>
<a name="line71"></a>  goog.messaging.LoggerClient.instance_ = this;
<a name="line72"></a>};
<a name="line73"></a>goog.inherits(goog.messaging.LoggerClient, goog.Disposable);
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * The singleton instance, if any.
<a name="line78"></a> * @type {goog.messaging.LoggerClient}
<a name="line79"></a> * @private
<a name="line80"></a> */
<a name="line81"></a>goog.messaging.LoggerClient.instance_ = null;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Sends a log message through the channel.
<a name="line86"></a> * @param {!goog.debug.LogRecord} logRecord The log message.
<a name="line87"></a> * @private
<a name="line88"></a> */
<a name="line89"></a>goog.messaging.LoggerClient.prototype.sendLog_ = function(logRecord) {
<a name="line90"></a>  var name = logRecord.getLoggerName();
<a name="line91"></a>  var level = logRecord.getLevel();
<a name="line92"></a>  var msg = logRecord.getMessage();
<a name="line93"></a>  var originalException = logRecord.getException();
<a name="line94"></a>
<a name="line95"></a>  var exception;
<a name="line96"></a>  if (originalException) {
<a name="line97"></a>    var normalizedException =
<a name="line98"></a>        goog.debug.normalizeErrorObject(originalException);
<a name="line99"></a>    exception = {
<a name="line100"></a>      &#39;name&#39;: normalizedException.name,
<a name="line101"></a>      &#39;message&#39;: normalizedException.message,
<a name="line102"></a>      &#39;lineNumber&#39;: normalizedException.lineNumber,
<a name="line103"></a>      &#39;fileName&#39;: normalizedException.fileName,
<a name="line104"></a>      // Normalized exceptions without a stack have &#39;stack&#39; set to &#39;Not
<a name="line105"></a>      // available&#39;, so we check for the existance of &#39;stack&#39; on the original
<a name="line106"></a>      // exception instead.
<a name="line107"></a>      &#39;stack&#39;: originalException.stack ||
<a name="line108"></a>          goog.debug.getStacktrace(goog.debug.Logger.prototype.log)
<a name="line109"></a>    };
<a name="line110"></a>
<a name="line111"></a>    if (goog.isObject(originalException)) {
<a name="line112"></a>      // Add messageN to the exception in case it was added using
<a name="line113"></a>      // goog.debug.enhanceError.
<a name="line114"></a>      for (var i = 0; &#39;message&#39; + i in originalException; i++) {
<a name="line115"></a>        exception[&#39;message&#39; + i] = String(originalException[&#39;message&#39; + i]);
<a name="line116"></a>      }
<a name="line117"></a>    }
<a name="line118"></a>  }
<a name="line119"></a>  this.channel_.send(this.serviceName_, {
<a name="line120"></a>    &#39;name&#39;: name, &#39;level&#39;: level.value, &#39;message&#39;: msg, &#39;exception&#39;: exception
<a name="line121"></a>  });
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/** @override */
<a name="line126"></a>goog.messaging.LoggerClient.prototype.disposeInternal = function() {
<a name="line127"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line128"></a>  goog.debug.LogManager.getRoot().removeHandler(this.publishHandler_);
<a name="line129"></a>  delete this.channel_;
<a name="line130"></a>  goog.messaging.LoggerClient.instance_ = null;
<a name="line131"></a>};
</pre>


</body>
</html>
